/** 
 * @param {WebGLRenderingContext} gl
 * @returns {WebGLProgram} 
 */
export function initShaderProgram(gl,vsSource,fsSource)
{
    const vertexShader=loadShader(gl,gl.VERTEX_SHADER,vsSource)
    const fragmentShader=loadShader(gl,gl.FRAGMENT_SHADER,fsSource)

    if(!(vertexShader&&fragmentShader)){
        alert("着色器对象初始化程序失败")
        return null
    }

    const program=gl.createProgram();
    gl.attachShader(program,vertexShader);
    gl.attachShader(program,fragmentShader);
    gl.linkProgram(program);
    if(!gl.getProgramParameter(program,gl.LINK_STATUS)){
        alert("着色器程序初始化程序失败")
        gl.deleteProgram(program)
        return null
    }
    return program;
}

/** 
 * @param {WebGLRenderingContext} gl 
 * @private 
 * */
function loadShader(gl,type,source){
    const shader = gl.createShader(type)
    gl.shaderSource(shader,source);

    gl.compileShader(shader);

    if(!gl.getShaderParameter(shader,gl.SHADER_TYPE)){
        gl.deleteShader(shader);
        return null
    }
     return shader;
}

/** 
 * @param {HTMLCanvasElement} canvas 
*/
export function getWebglContext(canvas){
    return canvas.getContext("webgl");
}


/** 
 * 设置元素大小占父dom的百分比，parent->null屏幕大小
 * @param {HTMLCanvasElement} element 
 * @param {Number} process  百分比(0~1)
 * @param {HTMLCanvasElement} parent 
*/
export function setSizePercentage(element,process,parent) {
    let parentWidht,parentHeight;

    if(parent){
        parentWidht = window.screen.width;
        parentHeight = window.screen.height;
    }else{
        parentWidht = parent.scrollWidth;
        parentHeight = parent.scrollHeight;
    }

    element.width  = parentWidht * process;
    element.height = parentHeight *process;
}
export const vsSource =
    `
precision mediump float;
    uniform mat4  orthMat;                  //Orthogonal model matrix :正交模型矩阵
    uniform mat4  modelMat; 
    uniform mat4  modelMat2; 
    attribute vec2 _vec2;
    attribute vec3 a_Position;
    uniform  float isModel;
    uniform vec4  color;  
    varying vec3  pointColor;     
    attribute vec3  inpointColor;     
    attribute float  pointSize;     
    void main() {
        
        if(isModel==0.0){
            gl_PointSize = pointSize;
            // gl_Position =   orthMat * modelMat * vec4(a_Position,1.0) ;
            vec3 pos =  vec3(a_Position.x+_vec2.x,a_Position.y+_vec2.y,a_Position.z);
            gl_Position =  orthMat * modelMat * vec4(pos,1.0);
            pointColor = inpointColor;
        }else{
            vec3 pos =  vec3(a_Position.x+_vec2.x,a_Position.y+_vec2.y,a_Position.z);
            gl_Position =  orthMat * modelMat2 * vec4(pos,1.0);
        }
        
    }
`;
export const fsSource = `
    precision mediump float;
    uniform  sampler2D texture;
    uniform  float isModel;
    uniform vec4  Modelcolor;    
    varying vec3  pointColor;     
    void main() {
        if(isModel==0.0){
            vec4 _color = texture2D(texture,gl_PointCoord);
            if(_color.w<.1){
                discard;
            }
            gl_FragColor =vec4(pointColor.x,pointColor.y,pointColor.z,_color.w);
        }else{
            gl_FragColor = Modelcolor;
        }
        


    }
`;